package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.FileParentIdMapping;
import com.tongtech.system.service.IFileParentIdMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 文件信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/file/mapping")
public class FileParentIdMappingController extends BaseController
{
    @Autowired
    private IFileParentIdMappingService fileParentIdMappingService;

    /**
     * 获取文件列表
     */

    @RequiresPermissions("file:info:list")
    @GetMapping("/list")
    public TableDataInfo list(FileParentIdMapping fileParentIdMapping)
    {
        startPage();
        List<FileParentIdMapping> list = fileParentIdMappingService.selectFileParentIdMappingList(fileParentIdMapping);
        return getDataTable(list);
    }

    /**
     * 根据文件编号获取详细信息
     */
    @RequiresPermissions("file:info:query")
    @GetMapping(value = "/{fileParentIdMappingId}")
    public AjaxResult getInfo(@PathVariable Long fileParentIdMappingId)
    {
        return success(fileParentIdMappingService.selectFileParentIdMappingById(fileParentIdMappingId));
    }

    /**
     * 新增文件
     */
    @RequiresPermissions("file:info:add")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody FileParentIdMapping fileParentIdMapping)
    {
       if(Objects.nonNull(fileParentIdMapping)){
           fileParentIdMapping.setCreateBy(SecurityUtils.getUsername());
       }
        return success(fileParentIdMappingService.insertFileParentIdMapping(fileParentIdMapping));
    }

    /**
     * 修改文件
     */
    @RequiresPermissions("file:info:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody FileParentIdMapping fileParentIdMapping)
    {
        if(Objects.nonNull(fileParentIdMapping)){
            fileParentIdMapping.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(fileParentIdMappingService.updateFileParentIdMapping(fileParentIdMapping));
    }

    /**
     * 删除文件
     */
    @RequiresPermissions("file:info:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileParentIdMappingId}")
    public AjaxResult remove(@PathVariable Long fileParentIdMappingId)
    {
        return toAjax(fileParentIdMappingService.deleteParentIdMappingFileById(fileParentIdMappingId));
    }
}
